package com.mgl.movesprite.game.model;

import java.util.ArrayList;

import org.andengine.entity.Entity;
import org.andengine.entity.scene.Scene;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.vbo.VertexBufferObjectManager;

import com.mgl.movesprite.EntitySpriteLevelBase;
import com.mgl.movesprite.game.factory.GameObjectFactory;
import com.mgl.movesprite.game.factory.GameType;

public abstract class Tower extends EntitySpriteLevelBase {

	protected ArrayList<GameType> typeBuildList;

	protected Shot shot;
	protected Boolean active = true;

	protected EntitySpriteLevelBase target;
	protected float timeToShot = 0;

	protected int buildPrice = 0;
	protected int sellPrice = 0;
	protected int range = 100;

	protected EntitySpriteLevelBase radio;

	public Tower(float pX, float pY, ITextureRegion pTextureRegion,
			VertexBufferObjectManager pVertexBufferObjectManager,
			GameObjectFactory gameObjectFactory) {
		super(pX, pY, pTextureRegion, pVertexBufferObjectManager,
				gameObjectFactory);

		typeBuildList = new ArrayList<GameType>();

		initTypeBuildList();

		radio = (EntitySpriteLevelBase) gameObjectFactory.createObject(GameType.RADIO, getWidth() / 2,
				getHeight() / 2);
		radio.setSize(range * 2, range * 2);
		radio.setPosition((getWidth() / 2) - range, (getHeight() / 2) - range);
		radio.setVisible(false);

		//this.attachChild(radio);

	}

	public abstract void initTypeBuildList();

	public abstract void updateTower(float pSecondsElapsed, Level level);

	public void update(float pSecondsElapsed, Level level) {
		try {

			/*
			 * timeToShot += pSecondsElapsed;
			 * 
			 * if (timeToShot > 12) { System.out.println("SHOOT");
			 * 
			 * timeToShot = 0; }
			 */
			if (!active) {
				return;
			}
			updateTower(pSecondsElapsed, level);

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public void setVisibleRadio(boolean visible) {
		radio.setVisible(visible);
	}

	private void shoot(Scene scene) {

		try {

			shot.setX(this.getX() + 10);
			shot.setY(this.getY() + 10);
			shot.setTarget(target);
			scene.attachChild(shot);

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public void setShot(Shot shot) {
		this.shot = shot;

	}

	public float getTimeToShot() {
		return timeToShot;
	}

	public void setTimeToShot(float timeToShot) {
		this.timeToShot = timeToShot;
	}

	public Shot getShot() {
		return shot;
	}

	public EntitySpriteLevelBase getTarget() {
		return target;
	}

	public void setTarget(EntitySpriteLevelBase target) {
		this.target = target;
	}

	public int getBuildPrice() {
		return buildPrice;
	}

	public void setBuildPrice(int buildPrice) {
		this.buildPrice = buildPrice;
	}

	public int getSellPrice() {
		return sellPrice;
	}

	public void setSellPrice(int sellPrice) {
		this.sellPrice = sellPrice;
	}

	public int getRange() {
		return range;
	}

	public void setRange(int range) {
		this.range = range;
		radio.setSize(range * 2, range * 2);
		radio.setPosition((getWidth() / 2) - range, (getHeight() / 2) - range);
	}

	public Boolean getActive() {
		return active;
	}

	public void setActive(Boolean active) {
		this.active = active;
	}

	public ArrayList<GameType> getTypeBuildList() {
		return typeBuildList;
	}

	public void setTypeBuildList(ArrayList<GameType> typeBuildList) {
		this.typeBuildList = typeBuildList;
	}

	public EntitySpriteLevelBase getRadio() {
		return radio;
	}

	public void setRadio(EntitySpriteLevelBase radio) {
		this.radio = radio;
	}

}
